package cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.pill;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityInHospitalPillMedicineDetailBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPillSingleBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.InHospitalPillMedicineDetailPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalPillMedicineDetailViewModel;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.functions.Consumer;

public class InHospitalPillMedicineDetailActivity extends BaseActivity<ActivityInHospitalPillMedicineDetailBinding,InHospitalPillMedicineDetailViewModel,InHospitalPillMedicineDetailPresenter> {
    private ArrayList<InHospitalPillSingleBeen> currently;
    static  String  DJJLID;
    List<InHospitalPillSingleBeen> inHospitalPatientBeens;
    private MDialog mDialog;
    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViewModel(viewModel);

        binding.medicineDetailTitle.titleTextTv.setText("包药机复核");
        binding.tvMedicineDetailPatientRoom.setText(presenter.DJBQMC);
        binding.medicineDetailTitle.titleBackLl.setVisibility(View.VISIBLE);

        binding.medicineDetailTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewModel.show()){
                    mDialog = CreateDialog.exitDialog(InHospitalPillMedicineDetailActivity.this, 5, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            CreateDialog.dismiss(InHospitalPillMedicineDetailActivity.this);
                            finish();
                        }
                    },"退出审核单后，需重新开始操作");
                }else {
                    finish();
                }
            }
        });
        binding.medicineDetailTitle.titleBackTv.setText("");
        binding.tvMedicineDetailChufanghao.setText("NO: "+presenter.DJJLID);
        binding.rvMedicineDetail.setLayoutManager(new LinearLayoutManager(this));
        binding.rvMedicineDetail.setPullRefreshEnabled(false);
        //开单科室  和 开单时间
        binding.btMedicineDetailStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO:传递页面展现的药品集合过去
                Intent intent = new Intent(InHospitalPillMedicineDetailActivity.this, InHospitalPillProgressActivity.class);
                //处方号
                intent.putExtra("DJJLID",presenter.DJJLID);
                intent.putExtra("total", viewModel.datas);
                startActivityForResult(intent, Contants.REQUEST_CODE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Contants.REQUEST_CODE:
                //TODO:用户未操作完全的药单 按BACK键返回  刷新界面数据     暂时不考虑
                currently = (ArrayList<InHospitalPillSingleBeen>) data.getExtras().getSerializable("currently");
                if(null != currently ){
                    viewModel.setWData(currently);
                }
                break;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if(viewModel.show()){
                mDialog = CreateDialog.exitDialog(this, 5, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CreateDialog.dismiss(InHospitalPillMedicineDetailActivity.this);
                        finish();
                    }
                },"退出审核单后，需重新开始操作");
            }
//            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected void onResume() {
        super.onResume();
        RxBus.getInstance().tObservable(Contants.INHOSPITAL_PILL_MEDICINE_DETAIL_RV_COUNT, List.class).subscribe(new Consumer<List<InHospitalPillSingleBeen>>() {
            @Override
            public void accept(List<InHospitalPillSingleBeen> putSingleBeens) throws Exception {
                //TODO:获取RecycleView的总条目数
                binding.tvMedicineDetailTotalNum.setText("共"+putSingleBeens.size()+"包药品");
                binding.tvMedicineDetailTime.setText("时间："+putSingleBeens.get(0).getBYSCSJ());

            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null != mDialog){
            mDialog.cancel();
        }
        PreferenceManager.getInstance().setHospitalCheckProgressTotal("0");
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_in_hospital_pill_medicine_detail;
    }

    @Override
    protected Class<InHospitalPillMedicineDetailPresenter> getPresenterClass() {
        return InHospitalPillMedicineDetailPresenter.class;
    }

    @Override
    protected Class<InHospitalPillMedicineDetailViewModel> getViewModelClass() {
        return InHospitalPillMedicineDetailViewModel.class;
    }
}
